package com.ruoyi.web.controller.system;

import java.util.List;

import com.ruoyi.common.core.domain.entity.BusinessSys;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.service.IBusinessSysService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 业务系统 Controller
 * 
 * @author ess
 * @date 2021-10-18
 */
@RestController
@RequestMapping("/system/businSys")
public class BusinessSysController extends BaseController
{
    @Autowired
    private IBusinessSysService businessSysService;

    /**
     * 查询业务系统 列表
     */
//    @PreAuthorize("@ss.hasPermi('system:sys:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusinessSys businessSys)
    {
        startPage();
        List<BusinessSys> list = businessSysService.selectBusinessSysList(businessSys);
        return getDataTable(list);
    }

    /**
     * 导出业务系统 列表
     */
    @PreAuthorize("@ss.hasPermi('system:sys:export')")
    @Log(title = "业务系统 ", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusinessSys businessSys)
    {
        List<BusinessSys> list = businessSysService.selectBusinessSysList(businessSys);
        ExcelUtil<BusinessSys> util = new ExcelUtil<BusinessSys>(BusinessSys.class);
        return util.exportExcel(list, "业务系统数据");
    }

    /**
     * 获取业务系统 详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:businSys:query')")
    @GetMapping(value = "/{bSysId}")
    public AjaxResult getInfo(@PathVariable("bSysId") String bSysId)
    {
        return AjaxResult.success(businessSysService.selectBusinessSysByBSysId(bSysId));
    }

    /**
     * 新增业务系统 
     */
    @PreAuthorize("@ss.hasPermi('system:businSys:add')")
    @Log(title = "业务系统 ", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusinessSys businessSys)
    {
        if (businessSys.getDataScope().equals("2")){
            if(businessSys.getSealTypeIds()==null||businessSys.getSealTypeIds().length==0){
                return AjaxResult.error("新增业务系统'" + businessSys.getbName() + "'失败，可用印章类型不能为空");

            }

        }
        return toAjax(businessSysService.insertBusinessSys(businessSys));
    }

    /**
     * 修改业务系统 
     */
    @PreAuthorize("@ss.hasPermi('system:businSys:edit')")
    @Log(title = "业务系统 ", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusinessSys businessSys)
    {
        if (businessSys.getDataScope().equals("2")){
            if(businessSys.getSealTypeIds()==null||businessSys.getSealTypeIds().length==0){
                return AjaxResult.error("新增业务系统'" + businessSys.getbName() + "'失败，可用印章类型不能为空");

            }
        }

        return toAjax(businessSysService.updateBusinessSys(businessSys));
    }

    /**
     * 删除业务系统 
     */
    @PreAuthorize("@ss.hasPermi('system:businSys:remove')")
    @Log(title = "业务系统 ", businessType = BusinessType.DELETE)
	@DeleteMapping("/{bSysIds}")
    public AjaxResult remove(@PathVariable String[] bSysIds)
    {
        return toAjax(businessSysService.deleteBusinessSysByBSysIds(bSysIds));
    }
}
